package core
{
	import com.greensock.TweenLite;
	
	import core.model.PSharedObject;
	import core.view.PlayListView;
	
	import flash.events.KeyboardEvent;
	import flash.events.NetStatusEvent;
	import flash.system.Security;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.utils.setTimeout;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;
	import org.aisy.display.USprite;

	public class Main extends USprite
	{
		private var _toAlpha0:*;
		private var _toAlpha1:*;
		
		public function Main()
		{
			try {
				Security.allowDomain("*");
				Security.allowInsecureDomain("*");
			}
			catch (error:*) {}
		}
		
		public function connect(im:*):void
		{
			initPlayer();
			im = null;
		}
		
		private function __import():void
		{
			_AISY_SCROLLER_SKIN;
			
			TweenLite;
		}
		
		protected function initPlayer():void
		{
			if (Ais.IMain.Swf.hasKey("iplayer_0") === -1) return;
			if (!PSharedObject.getInstance().data.p_list) {
				PSharedObject.getInstance().data.p_int = 0;
				PSharedObject.getInstance().data.p_list = [];
				PSharedObject.getInstance().flush();
			}
			
			if (PSharedObject.getInstance().data.p_list[PSharedObject.getInstance().data.p_int]) {
				TEvent.trigger("IPLAYER_0", "SET_URL", PSharedObject.getInstance().data.p_list[PSharedObject.getInstance().data.p_int][0]);
			}
			
			var a:Class = Ais.IMain.Swf.getClass("toalpha", "core.view.ToAlpha") as Class;
			if (a) {
				var _t0:* = Ais.IMain.Swf.getClass("iplayer_0", "core.view.ToolBarView").getInstance();
				var _t1:* = Ais.IMain.Swf.getClass("iplayer_0", "core.view.ProgressView0").getInstance();
				var _arr0:Array = [_t0, _t1];
				var _arr1:Array = [[0, 1, 0.5]];
				_toAlpha0 = new a(_t0, _arr0, _arr1);
				_toAlpha1 = new a(_t1, _arr0, _arr1);
				_t0 = null;
				_t1 = null;
				_arr0 = null;
				_arr1 = null;
				a = null;
			}
			
			__addEvent();
		}
		
		protected function __addEvent():void
		{
			Ais.IMain.stage.addEventListener(KeyboardEvent.KEY_DOWN, __stageKeyHandler);
			Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().addEventListener(NetStatusEvent.NET_STATUS, __netStatusHadler);
		}
		
		protected function __removeEvent():void
		{
			Ais.IMain.stage.removeEventListener(KeyboardEvent.KEY_DOWN, __stageKeyHandler);
			Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().removeEventListener(NetStatusEvent.NET_STATUS, __netStatusHadler);
		}
		
		protected function __play():void
		{
			var _p:uint = PSharedObject.getInstance().data.p_int;
			_p++;
			if (_p >= PSharedObject.getInstance().data.p_list.length) {
				_p = 0;
			}
			PSharedObject.getInstance().data.p_int = _p;
			PSharedObject.getInstance().flush();
			
			if (PSharedObject.getInstance().data.p_list[_p]) {
				TEvent.trigger("IPLAYER_0", "PLAY", PSharedObject.getInstance().data.p_list[_p][0]);
			}
			
			TEvent.trigger("PLAY_LISTOYITEM", "PLAY", _p);
		}
		
		protected function __stageKeyHandler(e:KeyboardEvent):void
		{
			if (e.target is TextField) return;
			if (e.keyCode === Keyboard.P) {
				if (TEvent.hasTrigger("PLAY_LIST")) {
					TEvent.trigger("UP_WINDOW_NEW", "CLEAR", {"name": "PLAY_LIST"});
				}
				else {
					new PlayListView();
				}
			}
			e = null;
		}
		
		protected function __netStatusHadler(e:NetStatusEvent):void
		{
			switch (e.info.code) {
				case "NetStream.Play.Stop":
					setTimeout(__play, 1000);
					break;
			}
			e = null;
		}
		
		override public function clear():void
		{
			__removeEvent();
			if (_toAlpha0) _toAlpha0.clear();
			if (_toAlpha1) _toAlpha1.clear();
			_toAlpha0 = null;
			_toAlpha1 = null;
			super.clear();
		}
		
	}
}